const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const bodyparser = require("koa-bodyparser");
const { query } = require("./db/index");
const connect = require("./db/index");

router.get("/lists", async (ctx) => {
  let res = await new Promise((resolve, rejects) => {
    connect.query(`select * from user `, (err, data) => {
      if (!err) {
        resolve(data);
      } else {
        rejects("失败");
      }
    });
  });

  ctx.body = {
    code: 0,
    mes: "lists",
    data: res,
  };
});

router.post("/login", async (ctx) => {
    console.log(ctx.request.body);
  let { name, pwd } = ctx.request.body;
  let res = await new Promise((resolve, rejects) => {
    connect.query(`select * from user where name='${name}'`, (err, data) => {
      if (!err) {
        resolve(data);
      } else {
        rejects("失败");
      }
    });
  });

  ctx.body = {
    code: 0,
    msg: "成功111",
    res,
  };
});


app.use(bodyparser());
app.use(router.routes());
app.listen(3001);
